﻿using System;
using System.IO;

namespace Azure.Online.Proxy.Server {
    public class HttpServerResponseStream : Stream {
        protected readonly HttpServerContext _context;
        protected readonly Stream _inner;
        private readonly long _length;
        private long _position;

        public HttpServerResponseStream(HttpServerContext context, Stream inner, long length) {
            _context = context;
            _inner = inner;
            _length = length;
        }

        public override void Flush() {
            _inner.Flush();
        }

        public override long Seek(long offset, SeekOrigin origin) {
            throw new InvalidOperationException();
        }

        public override void SetLength(long value) {
            throw new InvalidOperationException();
        }

        public override int Read(byte[] buffer, int offset, int count) {
            throw new InvalidOperationException();
        }

        public override void Write(byte[] buffer, int offset, int count) {
            _inner.Write(buffer, offset, count);
            _position += count;
        }

        public override bool CanRead {
            get { return false; }
        }

        public override bool CanSeek {
            get { return false; }
        }

        public override bool CanWrite {
            get { return true; }
        }

        public override long Length {
            get { return _length; }
        }

        public override long Position {
            get { return _position; }
            set {
                throw new InvalidOperationException();
            }
        }
    }
}
